package edu.umd.fosite.demos;

import java.awt.*;
import java.awt.image.BufferedImage;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLJPanel;
import javax.swing.*;

import com.jogamp.opengl.util.FPSAnimator;

import edu.umd.fosite.ui.UIUtils;

public abstract class GLDemo implements Demo {

	public void run() {
		JFrame frame = new JFrame("Fosite Demo");
		frame.setResizable(false);

		// Set a blank cursor
		BufferedImage cursorImg = new BufferedImage(16, 16,
				BufferedImage.TYPE_INT_ARGB);
		Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
				cursorImg, new Point(0, 0), "blank cursor");
		frame.getContentPane().setCursor(blankCursor);

		GLCapabilities capabilities = new GLCapabilities(
				GLProfile.get(GLProfile.GL2));
		capabilities.setDoubleBuffered(true);
		//capabilities.setSampleBuffers(true);
		//capabilities.setNumSamples(1);

		GLJPanel canvas = new GLJPanel(capabilities);
		canvas.addGLEventListener(new GLEventListener() {

			@Override
			public void reshape(GLAutoDrawable drawable, int x, int y, int w,
					int h) {
				GLDemo.this.reshape(drawable.getGL().getGL2(), x, y, w, h);
			}

			@Override
			public void init(GLAutoDrawable drawable) {
				GLDemo.this.init(drawable.getGL().getGL2());
			}

			@Override
			public void dispose(GLAutoDrawable drawable) {
				GLDemo.this.dispose(drawable.getGL().getGL2());
			}

			@Override
			public void display(GLAutoDrawable drawable) {
				GLDemo.this.display(drawable.getGL().getGL2());
			}
		});

		frame.add(canvas);
		frame.setSize(800, 500);
		UIUtils.centerOnScreen(frame);
		frame.setBackground(Color.white);
		frame.setVisible(true);

		FPSAnimator animator = new FPSAnimator(canvas, 60);
		animator.start();
	}

	protected abstract void reshape(GL2 gl, int x, int y, int width, int height);

	protected abstract void init(GL2 gl);

	protected abstract void dispose(GL2 gl);

	protected abstract void display(GL2 gl);

}
